package cn.hill4j.tool.spring.ext.mvc;

import cn.hill4j.tool.spring.ext.mvc.decrypt.RequestDecStrategy;
import cn.hill4j.tool.spring.ext.mvc.encryption.ResponseEncStrategy;
import cn.hill4j.tool.spring.ext.mvc.exception.RequestDesRegisterException;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
/**
 * ClassName: RequestDecStrategyFactory
 * Description: 响应结果加密工厂
 * @author hillchen
 * Date: 2023/8/13
 * @version: 1.0
 */
public final class ResponseEncStrategyFactory {
    private ResponseEncStrategyFactory(){};
    private static Map<String, ResponseEncStrategy> STRATEGY_MAP = new ConcurrentHashMap<>();

    /**
     * @author hillchen
     * Description: 注册响应结果加密策略
     * Date: 2023/8/13 00:44
     * @param strategyType 策略业务类型标志
     * @param strategy 策略处理器
     * @return void
     **/
    static void registerStrategy(String strategyType, ResponseEncStrategy strategy){
        if (STRATEGY_MAP.containsKey(strategyType)){
            throw new RequestDesRegisterException(String.format("%s解析策略以及注册,不能重复注册",strategyType));
        }
        STRATEGY_MAP.putIfAbsent(strategyType, strategy);
    }

    /**
     * @author hillchen
     * Description: 获取响应结果加密策略
     * Date: 2023/8/13 00:45
     * @param strategyType 策略业务类型标志
     * @return void
     **/
    static ResponseEncStrategy selectStrategy(String strategyType){
        return STRATEGY_MAP.get(strategyType);
    }
}
